package com.ycy.leetcode;

/**
 * 大数想加
 */
public class BigDataAdd {

  public static void main(String[] args) {

    System.out.println(444449999 % 10000);

    Num num = new Num(4444444);
    Num num1 = new Num(4444444);

    num.add(num1);

    System.out.println(num);

    int i = 0;
    while (true) {
      try {
        Thread.sleep(1000);
        System.out.println(i++);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }

  }

  static class Num {

    public static int ArrayLen = 500;
    private int[] num = new int[ArrayLen];

    public void multiply(Num b) {
      int jinwei = 0, l = (b.len() + 3) / 4;
      Num result = new Num(), temp = new Num();
      for (int i = 0; i < l; i++) {
        temp.clear();
        for (int j = 0; j <= (this.len() + 3) / 4; j++) {
          int t = (this.num[j] * b.num[i] + jinwei) % 10000;
          jinwei = (this.num[j] * b.num[i] + jinwei) / 10000;
          temp.num[i + j] = t;
        }
        result.add(temp);
      }
      this.clone(result);
    }

    public void clear() {
      for (int i = 0; i < ArrayLen; i++) {
        this.num[i] = 0;
      }
    }

    //打印变量
    public int compare(Num b) {
      int result = 0;
      for (int i = ArrayLen - 1; i >= 0; i--) {
        if (this.num[i] != b.num[i]) {
          result = this.num[i] - b.num[i];
          break;
        } else if (i == 0) {
          result = 0;
        }
      }
      return result;
    }

    public void print() {
      int i = ArrayLen - 1;
      while (num[i] == 0 && i > 0)
        i--;
      System.out.print("num=" + num[i]);
      while (--i >= 0) {
        if (num[i] < 10) {
          System.out.print("000");
          System.out.print(num[i]);
        } else if (num[i] < 100) {
          System.out.print("00");
          System.out.print(num[i]);
        } else if (num[i] < 1000) {
          System.out.print("0");
          System.out.print(num[i]);
        } else {
          System.out.print(num[i]);
        }

      }
      System.out.println(" ");
    }

    //返回此num对象存储的整数的长度
    public int len() {
      int len = 0;
      for (int i = 499; i >= 0; i--) {
        if (num[i] != 0) {
          if (num[i] < 10)
            len = i * 4 + 1;
          else if (num[i] < 100)
            len = i * 4 + 2;
          else if (num[i] < 1000)
            len = i * 4 + 3;
          else
            len = i * 4 + 4;
          break;
        } else if (i == 0) {
          len = 1;
        }
      }
      return len;
    }

    public void clone(Num a) {
      for (int i = 0; i < ArrayLen; i++) {
        num[i] = a.num[i];
      }
    }

    public void add(Num b) {
      for (int i = 0, jinwei = 0; i < ArrayLen; i++) {

        int t = (this.num[i] + b.num[i] + jinwei) % 10000;
        jinwei = (this.num[i] + b.num[i] + jinwei) / 10000;
        this.num[i] = t;
      }
    }

    public Num() {
    }

    public Num(int input) {
      this.setNum(input);
    }

    public Num(String str) {
      this.setNum(str);
    }

    public void setNum(int input) {
      int i = 0;
      this.clear();
      while (input > 0) {
        this.num[i] = input % 10000;
        input /= 10000;
        i++;
      }
    }

    public void setNum(String str) {
      this.clear();
      if (str.length() % 4 != 0) {
        int i = 4 - str.length() % 4;
        while (i-- > 0) {
          str = "0".concat(str);
        }
      }
      int len = str.length();
      for (int i = 0; i < len / 4; i++) {
        this.num[i] = Integer.parseInt(str.substring(len - i * 4 - 4, len - i * 4));
      }
    }

    //运算a^p,存储在a中
    public void pow(int p) {
      if (p == 0) {
        this.clear();
        this.num[0] = 1;
      } else if (p > 0) {
        Num t = new Num();
        t.clone(this);
        for (int i = 1; i < p; i++) {
          this.multiply(t);
        }
      }
    }

    public String toString() {
      String s = new String("num=");
      int i = ArrayLen - 1;
      while (num[i] == 0 && i > 0)
        i--;
      s += num[i];
      while (--i >= 0) {
        if (num[i] < 10) {
          s += "000";
          s += num[i];
        } else if (num[i] < 100) {
          s.concat("00");
          s += num[i];
        } else if (num[i] < 1000) {
          s.concat("0");
          s += num[i];
        } else {
          s += num[i];
        }
      }
      return s;
    }
  }
}


